package com.example.switchinactivitiesanimation.fragments.views;

import com.example.switchinactivitiesanimation.CrossFadingActivity;
import com.example.switchinactivitiesanimation.MainActivity;
import com.example.switchinactivitiesanimation.R;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class FadeInFadeOutActivityFragment extends Fragment implements
		AnimationListener {

	Button fadein, fadeout, nextActiviy, back;
	Animation animFadeIn, animFadeOut;
	TextView fadeinfadeoutthistext;

	public FadeInFadeOutActivityFragment() {

	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_fadein_fadeout,
				container, false);

		initViewComponents(rootView);

		return rootView;
	}

	// Initialize view components
	public void initViewComponents(View rootView) {
		viewComponents(rootView);
		actionViewComponents(rootView);
	}

	// collection of components
	public void viewComponents(View rootView) {

		nextActiviy = (Button) rootView.findViewById(R.id.nextAct);
		back = (Button) rootView.findViewById(R.id.back);

		fadein = (Button) rootView.findViewById(R.id.fadein);
		fadeout = (Button) rootView.findViewById(R.id.fadeout);

		fadeinfadeoutthistext = (TextView) rootView
				.findViewById(R.id.fadein_fadeout_this_text);

	}

	// components with actions

	public void actionViewComponents(View rootView) {

		fadein.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Toast.makeText(getActivity(), "fadein!", Toast.LENGTH_SHORT)
						.show();
				animationFadeIn(v.getContext());
			}

		});

		fadeout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Toast.makeText(getActivity(), "fadeout!", Toast.LENGTH_SHORT)
						.show();
				animationFadeOut(v.getContext());

			}

		});

		nextActiviy.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent i = new Intent(v.getContext(), CrossFadingActivity.class);
				getActivity().startActivity(i);
				getActivity().overridePendingTransition(R.animator.grow_from_middle,
						R.animator.shirnk_to_middle);

			}

		});

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent i = new Intent(v.getContext(), MainActivity.class);
				startActivity(i);
				getActivity().overridePendingTransition(
						R.animator.push_down_in, R.animator.push_down_out);

			}

		});

	}

	// Fade in method

	public void animationFadeIn(Context context) {

		animFadeIn = AnimationUtils.loadAnimation(getActivity(),
				R.animator.fade_in);
		fadeinfadeoutthistext.startAnimation(animFadeIn);
		animFadeIn.setAnimationListener(this);

	}

	// Fade out method

	public void animationFadeOut(Context context) {

		animFadeOut = AnimationUtils.loadAnimation(getActivity(),
				R.animator.fade_out);
		fadeinfadeoutthistext.startAnimation(animFadeOut);
		animFadeOut.setAnimationListener(this);
	}

	// animation listener methods onAnimationStart

	@Override
	public void onAnimationStart(Animation animation) {

		if (animation == animFadeIn) {
			Toast.makeText(getActivity(), "animation START on fade in",
					Toast.LENGTH_SHORT).show();
		} else if (animation == animFadeOut) {

			Toast.makeText(getActivity(), "animation START on fade out",
					Toast.LENGTH_SHORT).show();

		}

	}

	// animation listener methods onAnimationEnd

	@Override
	public void onAnimationEnd(Animation animation) {

		if (animation == animFadeIn) {
			Toast.makeText(getActivity(), "animation END on fade in",
					Toast.LENGTH_SHORT).show();
		} else if (animation == animFadeOut) {

			Toast.makeText(getActivity(), "animation END on fade out",
					Toast.LENGTH_SHORT).show();

		}

	}

	// animation listener methods onAnimationRepeat

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onStart() {
		super.onStart();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onDetach() {
		super.onDetach();
	}
}
